/*
 *接口编写处...
 *如果接口需要做Action的权限验证，请在Action上使用属性
 *如: [ApiActionPermission("GoodsBath",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.demo.IServices;
using VOL.Core.Filters;

namespace VOL.demo.Controllers
{
    public partial class GoodsBathController
    {
        private readonly IGoodsBathService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public GoodsBathController(
            IGoodsBathService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 添加货品批次信息
        /// </summary>
        /// <param name="goodsBatch">货品批次信息</param>
        /// <returns></returns>
        [HttpPost("AddGoodsBatch")]
        public IActionResult AddGoodsBatch([FromBody] GoodsBatchModel goodsBatch)
        {
            try
            {
                if (goodsBatch == null)
                {
                    return Json(new { success = false, message = "货品批次信息不能为空" });
                }

                var result = _service.AddGoodsBatch(goodsBatch);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message, data = result.Data });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 批量添加货品批次信息
        /// </summary>
        /// <param name="goodsBatches">货品批次列表</param>
        /// <returns></returns>
        [HttpPost("BatchAddGoodsBatch")]
        public IActionResult BatchAddGoodsBatch([FromBody] List<GoodsBatchModel> goodsBatches)
        {
            try
            {
                if (goodsBatches == null || goodsBatches.Count == 0)
                {
                    return Json(new { success = false, message = "货品批次信息不能为空" });
                }

                var result = _service.BatchAddGoodsBatch(goodsBatches);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message, data = result.Data });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 根据货品ID和仓库ID获取批次信息
        /// </summary>
        /// <param name="goodsId">货品ID</param>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns></returns>
        [HttpGet("GetBatchesByGoodsAndWarehouse")]
        public IActionResult GetBatchesByGoodsAndWarehouse(long goodsId, long warehouseId)
        {
            try
            {
                var result = _service.GetBatchesByGoodsAndWarehouse(goodsId, warehouseId);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 根据批次号获取批次信息
        /// </summary>
        /// <param name="batchNo">批次号</param>
        /// <returns></returns>
        [HttpGet("GetBatchByBatchNo")]
        public IActionResult GetBatchByBatchNo(string batchNo)
        {
            try
            {
                if (string.IsNullOrEmpty(batchNo))
                {
                    return Json(new { success = false, message = "批次号不能为空" });
                }

                var result = _service.GetBatchByBatchNo(batchNo);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 更新货品批次信息
        /// </summary>
        /// <param name="goodsBatch">货品批次信息</param>
        /// <returns></returns>
        [HttpPost("UpdateGoodsBatch")]
        public IActionResult UpdateGoodsBatch([FromBody] GoodsBatchModel goodsBatch)
        {
            try
            {
                if (goodsBatch == null || goodsBatch.Id <= 0)
                {
                    return Json(new { success = false, message = "货品批次ID无效" });
                }

                var result = _service.UpdateGoodsBatch(goodsBatch);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 删除货品批次信息
        /// </summary>
        /// <param name="id">批次ID</param>
        /// <returns></returns>
        [HttpDelete("DeleteGoodsBatch/{id}")]
        public IActionResult DeleteGoodsBatch(long id)
        {
            try
            {
                if (id <= 0)
                {
                    return Json(new { success = false, message = "批次ID无效" });
                }

                var result = _service.DeleteGoodsBatch(id);
                if (result.Status)
                {
                    return Json(new { success = true, message = result.Message });
                }
                else
                {
                    return Json(new { success = false, message = result.Message });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 获取即将过期的批次信息
        /// </summary>
        /// <param name="days">提前天数（默认30天）</param>
        /// <returns></returns>
        [HttpGet("GetExpiringSoonBatches")]
        public IActionResult GetExpiringSoonBatches(int days = 30)
        {
            try
            {
                var result = _service.GetExpiringSoonBatches(days);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 根据仓库ID获取所有批次信息
        /// </summary>
        /// <param name="warehouseId">仓库ID</param>
        /// <returns></returns>
        [HttpGet("GetBatchesByWarehouse")]
        public IActionResult GetBatchesByWarehouse(long warehouseId)
        {
            try
            {
                if (warehouseId <= 0)
                {
                    return Json(new { success = false, message = "仓库ID无效" });
                }

                var result = _service.GetBatchesByWarehouse(warehouseId);
                return Json(new { success = true, data = result });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }
    }
}


